/*******************************************************************************
 * Boeing Proprietary, Confidential and/or Trade Secret
 * 
 * Copyright 2006, 2007 The Boeing Company
 * 
 * Unpublished Work. All Rights Reserved.
 ******************************************************************************/
package controls;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;

public class Section extends Composite
{

	private Button		downButton;

	private Button		upButton;

	private Composite	contentsComposite;

	private Composite	headingComposite;

	private Link		link;

	private Label		headingLabel;

	public Section( Composite parent, int style )
	{
		super(parent, SWT.BORDER);
		setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.verticalSpacing = 0;
		setLayout(gridLayout);

		headingComposite = new Composite(this, SWT.NONE);
		headingComposite.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		headingComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 4;
		headingComposite.setLayout(gridLayout_1);
		// Add 'click to enable center mouse wheel scroll' support
		//Utils.addMouseScrollSupport(headingComposite);
		// Enable the Expand/Collapse function on click of header composite
		headingComposite.addMouseListener(new MouseAdapter()
		{
			public void mouseDown(MouseEvent e)
			{
				if (upButton.getVisible())
				{
					upButton.notifyListeners(SWT.Selection, new Event());
				}
				else if (downButton.getVisible())
				{
					downButton.notifyListeners(SWT.Selection, new Event());
				}
			}
		});

		headingLabel = new Label(headingComposite, SWT.NONE);
		headingLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				true));
		headingLabel.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		headingLabel.setText("Section Heading");
		// Change the heading label font weight without changing any other
		// property
		final FontData[] fontData = headingLabel.getFont().getFontData();
		for (FontData element : fontData)
		{
			headingLabel.setFont(new Font(Display.getDefault(), element
					.getName(), element.getHeight() + 2, SWT.BOLD));
		}

		link = new Link(headingComposite, SWT.NONE);
		link
				.setBackground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		link.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
		link.setText("");

		upButton = new Button(headingComposite, SWT.ARROW);
		upButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				// Hide the contents composite
				contentsComposite.setVisible(false);
				((GridData) contentsComposite.getLayoutData()).exclude = true;

				// Hide this button and show the down button
				downButton.setVisible(true);
				((GridData) downButton.getLayoutData()).exclude = false;
				upButton.setVisible(false);
				((GridData) upButton.getLayoutData()).exclude = true;
				headingComposite.layout();
				Section.this.getParent().layout();
			}
		});
		upButton.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		final GridData gd_upButton = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false);
		upButton.setLayoutData(gd_upButton);
		upButton.setText("button");

		downButton = new Button(headingComposite, SWT.ARROW | SWT.DOWN);
		downButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				// Show the contents composite
				contentsComposite.setVisible(true);
				((GridData) contentsComposite.getLayoutData()).exclude = false;

				// Hide this button and show the up button
				downButton.setVisible(false);
				((GridData) downButton.getLayoutData()).exclude = true;
				upButton.setVisible(true);
				((GridData) upButton.getLayoutData()).exclude = false;
				headingComposite.layout();
				Section.this.getParent().layout();
			}
		});
		final GridData gd_downButton = new GridData(SWT.RIGHT, SWT.CENTER,
				false, false);
		downButton.setLayoutData(gd_downButton);
		downButton.setText("button");

		contentsComposite = new Composite(this, SWT.NONE);
		contentsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true));
		contentsComposite.setLayout(new RowLayout());
		// Add 'click to enable center mouse wheel scroll' support
		//Utils.addMouseScrollSupport(contentsComposite);

		// Keep the section expanded on load
		downButton.notifyListeners(SWT.Selection, new Event());
	}

	public void setText(String text)
	{
		headingLabel.setText(text == null ? "" : text);
		headingComposite.layout();
	}

	public String getText()
	{
		return headingLabel.getText();
	}

	public void setHyperlinkText(String text)
	{
		text = (text == null) ? "" : text;
		link.setText("<a>" + text + "</a>");
		headingComposite.layout();
	}

	public void addHyperlinkSelectionListener(SelectionListener listener)
	{
		link.addSelectionListener(listener);
	}

	public void removeHyperlinkSelectionListener(SelectionListener listener)
	{
		link.removeSelectionListener(listener);
	}

	public Composite getControl()
	{
		return contentsComposite;
	}

}
